package com.xmy.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.study.entity.EduPeriod;
import com.xmy.study.service.IEduPeriodService;
import com.xmy.study.util.Code;
import com.xmy.study.util.IdWorker;
import com.xmy.study.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * <p>
 * 学段 前端控制器
 * </p>
 *
 * @author Coly
 * @since 2023-05-29
 */
@RestController
@RequestMapping("/api/admin/eduPeriod")
@Api("学段控制器")
public class EduPeriodController {
    @Autowired
    IdWorker idWorker;

    @Autowired
    IEduPeriodService eduPeriodService;

    @PostMapping("newPeriod")
    @ApiOperation("新增学段信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "学段名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sort", value = "排序号", required = false, dataType = "String")
    })
    public Result newPeriod(@RequestBody Map reMap) {
        String name = (String) reMap.get("name");
        String sort = (String) reMap.get("sort");

        EduPeriod eduPeriod = new EduPeriod();
        eduPeriod.setId(idWorker.nextId());
        eduPeriod.setName(name);

        if (StringUtils.isNotBlank(sort)) {
            Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
            Boolean sortB = pattern.matcher(sort).matches();
            if (!sortB) {
                return new Result(Code.SAVE_ERROR, Code.SAVE_ERROR_MSG, "请正确输入排序号，例如：1");
            }
            eduPeriod.setSort(Integer.valueOf(sort));
        }

        eduPeriod.setStatus(1);
        eduPeriodService.save(eduPeriod);
        return new Result(Code.OK, null, "学段新增成功~");
    }

    @GetMapping("getPeriod")
    @ApiOperation("查询学段信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "学段名", required = true, dataType = "String")
    })
    public Result getPeriod(@RequestParam Map reMap) {
        String name = (String) reMap.get("name");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.orderByAsc("sort");
        List<EduPeriod> eduPeriodList = eduPeriodService.list(queryWrapper);
        return new Result(Code.OK, eduPeriodList, "学段信息查询成功~");
    }

    @DeleteMapping("delPeriod")
    @ApiOperation("删除学段信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "名称", required = true, dataType = "String")
    })
    public Result delPeriod(@RequestParam("id") String id) {
        EduPeriod eduPeriod = eduPeriodService.getById(id);
        if (Objects.isNull(eduPeriod)) {
            return new Result(Code.EXP_ERROR, Code.EXP_MSG, "该学段不存在");
        }
        eduPeriodService.removeById(id);
        return new Result(Code.OK, null, "学段信息删除成功~");
    }

    @PostMapping("updatePeriod")
    @ApiOperation("修改学段信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "学段名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sort", value = "排序号", required = false, dataType = "String")
    })
    public Result updatePeriod(@RequestBody Map reMap) {
        String id = (String) reMap.get("id");
        String name = (String) reMap.get("name");
        String sort = (String) reMap.get("sort");

        EduPeriod eduPeriod = eduPeriodService.getById(id);
        if (Objects.isNull(eduPeriod)) {
            return new Result(Code.EXP_ERROR, Code.EXP_MSG, "该学段不存在");
        }

        eduPeriod.setName(name);

        if (StringUtils.isNotBlank(sort)) {
            //判断是否为数字
            Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
            Boolean sortB = pattern.matcher(sort).matches();
            if (!sortB) {
                return new Result(Code.SAVE_ERROR, Code.SAVE_ERROR_MSG, "请正确输入排序号，例如：1");
            }
            eduPeriod.setSort(Integer.valueOf(sort));
        }

        eduPeriodService.updateById(eduPeriod);
        return new Result(Code.OK, null, "学段信息修改成功~");
    }
}
